package com.bjpowernode.crm.web.controller;

import com.bjpowernode.crm.pojo.Type;
import com.bjpowernode.crm.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("type")
public class TypeController {
    @Resource
    private TypeService typeService;


    //查询字典数据类型是否重复
    @RequestMapping("getCode.do")
    @ResponseBody
    public boolean getCode(String code){
        Type type = typeService.getCode(code);

        if(type !=null){
            return false;
        }else {
            return true;
        }
    }


    //添加数据字典类型
    @RequestMapping("addDictionaryType.do")

    public String addDictionaryType(Type type){

         typeService.addDictionaryType(type);
        return "redirect:/settings/dictionary/index.html";

    }



    //查询数据字典类型：
    @RequestMapping("getDictionaryType.do")
    @ResponseBody
    public List<Type> getType(){
        return typeService.getType();
    }



    //编辑数据字典类型:数据回显功能
    @RequestMapping("edit.do")
    public ModelAndView getTypeByCode(String code){
        System.out.println("kk");
        ModelAndView mv = new ModelAndView();
        Type type = typeService.getCode(code);
        mv.addObject("type",type);
        mv.setViewName("settings/dictionary/type/edit");
        return mv;
    }


    //更新数据字典
    @RequestMapping("editType.do")
    public String editType(Type type){
        typeService.editType(type);
        return "redirect:/settings/dictionary/type/index.html";
    }


    //删除字典类型前：验证字典code是否被引用为typeValue的外键
    @RequestMapping("checkTypeCode.do")
    @ResponseBody
    public String[] findToTypeCode(String[] codes){
        System.out.println("###################");
        System.out.println(Arrays.toString(codes));
       return typeService.findToTypeCode(codes);


    }




    //删除功能
    @RequestMapping("delType.do")
    public String delType(String[] codes){
        System.out.println(codes);
        typeService.delType(codes);
        return "redirect:/settings/dictionary/type/index.html";
    }



}
